package com.zoe.onelink.governance.autoconfigure;

import com.alibaba.cloud.nacos.NacosConfigManager;
import com.zoe.onelink.governance.gateway.model.SmartGatewayRoute;
import com.zoe.onelink.governance.gateway.route.GatewayDynamicRouteGenerator;
import com.zoe.onelink.governance.gateway.route.generator.AnnotationGatewayDynamicRouteGenerator;
import com.zoe.onelink.governance.gateway.route.generator.NativeGatewayDynamicRouteGenerator;
import com.zoe.onelink.governance.gateway.route.generator.SmartGatewayDynamicRouteGenerator;
import com.zoe.onelink.governance.gateway.route.publisher.GatewayDynamicRoutePublisher;
import com.zoe.onelink.governance.property.OnelinkGovernanceProperties;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;

import java.util.List;

/**
 * <p>标题: </p>
 * <p>描述: </p>
 * <p>版权: Copyright (c) 2022</p>
 * <p>公司: 智业软件股份有限公司</p>
 *
 * @version: 1.0
 * @author: ruansheng
 * @date: 2022-10-18
 */
@Configuration(proxyBeanMethods = false)
@ConditionalOnClass(NacosConfigManager.class)
@Slf4j
public class OnelinkGovernanceGatewayAutoConfiguration {

    @Configuration(proxyBeanMethods = false)
    @ConditionalOnProperty(name = "onelink.governance.gateway.dynamic-route-publish.enabled", matchIfMissing = true)
    static class RoutePublishAutoConfiguration {

        @Bean
        @ConditionalOnBean(NacosConfigManager.class)
        public GatewayDynamicRoutePublisher gatewayDynamicRoutePublisherListener(List<GatewayDynamicRouteGenerator> gatewayDynamicRouteGenerators,
                                                                                 Environment env,
                                                                                 NacosConfigManager nacosConfigManager,
                                                                                 OnelinkGovernanceProperties onelinkGovernanceProperties) {
            log.info("[OneLinkFrm] 开启网关动态路由发布功能");
            String serviceName = env.getProperty("spring.application.name");
            return new GatewayDynamicRoutePublisher(gatewayDynamicRouteGenerators, serviceName, nacosConfigManager, onelinkGovernanceProperties.getGateway().getDynamicRoutePublish());
        }

        @Bean
        @ConditionalOnBean(NacosConfigManager.class)
        public GatewayDynamicRouteGenerator nativeGatewayDynamicRoutePublisher(OnelinkGovernanceProperties onelinkGovernanceProperties,
                                                                               Environment environment) {
            return new NativeGatewayDynamicRouteGenerator(onelinkGovernanceProperties, environment);
        }

        @Bean
        @ConditionalOnBean(NacosConfigManager.class)
        public GatewayDynamicRouteGenerator smartGatewayDynamicRoutePublisher(OnelinkGovernanceProperties onelinkGovernanceProperties) {
            List<SmartGatewayRoute> smartGatewayRoutes = onelinkGovernanceProperties.getGateway().getDynamicRoutePublish().getRoutes();
            return new SmartGatewayDynamicRouteGenerator(smartGatewayRoutes);
        }

        @Bean
        @ConditionalOnBean(NacosConfigManager.class)
        public GatewayDynamicRouteGenerator annotationGatewayDynamicRouteGenerator(ApplicationContext applicationContext) {
            return new AnnotationGatewayDynamicRouteGenerator(applicationContext);
        }

    }


}
